package com.ebring.shareEducation.utils;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.*;

import static org.springframework.util.StreamUtils.BUFFER_SIZE;

public class FTPUtils {

    public static boolean uploadFile(String url, int port, String username, String password, String path, String filename, InputStream input) throws IOException {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);         //连接FTP服务器
            ftp.login(username, password);  //登录
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return success;
            }
            ftp.setControlEncoding("UTF-8");
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();
            ftp.changeWorkingDirectory(path);
            ftp.storeFile(new String(filename.getBytes("UTF-8"), "iso-8859-1"), input);

            input.close();
            ftp.logout();
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                ftp.disconnect();
            }
        }
        return success;
    }

    public static byte[] downFile(String url, int port, String username, String password, String remotePath, String fileName) throws IOException {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);
            ftp.login(username, password);          //登录
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return byteStream.toByteArray();
            }
            ftp.setControlEncoding("UTF-8");
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();
            ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
            String[] fs = ftp.listNames();
            for (String ff : fs) {
                String ftpName = ff;
                if (ftpName.equals(fileName)) {
                    try (InputStream is = ftp.retrieveFileStream(new String(ftpName.getBytes("UTF-8"), "ISO-8859-1"))) {
                        byte[] buffer = new byte[BUFFER_SIZE];
                        int len = -1;
                        while ((len = is.read(buffer, 0, buffer.length)) != -1) {
                            byteStream.write(buffer, 0, len);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                ftp.disconnect();
            }
        }
        return byteStream.toByteArray();
    }

    /**
     * 删除文件 *
     *
     * @param pathname FTP服务器保存目录 *
     * @param filename 要删除的文件名称 *
     * @return true 或 false
     */
    public static boolean deleteFile(String url, int port, String username, String password, String pathname, String filename) throws IOException {
        boolean flag = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);         //连接FTP服务器
            ftp.login(username, password);  //登录
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return flag;
            }
            ftp.setControlEncoding("UTF-8");
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();
            ftp.changeWorkingDirectory(pathname);
            ftp.deleteFile(new String(filename.getBytes("UTF-8"), "iso-8859-1"));
            ftp.logout();
            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                ftp.disconnect();
            }
        }
        return flag;
    }
}
